package com.mingqi.enetconnected.primary.personcentre;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hedgehog.ratingbar.RatingBar;
import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.base.BaseFragment;
import com.mingqi.enetconnected.primary.MainActivity;
import com.mingqi.enetconnected.primary.personcentre.adapter.MyPersonCentreAdapter;
import com.mingqi.enetconnected.util.DeviceUtils;
import com.mingqi.enetconnected.util.hint.ToastUtils;
import com.mingqi.enetconnected.util.resource.ResourceUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 个人中心模块
 * Created by li on 2018/7/30.
 * WeChat 18571658038
 * author LiWei
 */

public class PersonCentreFragment extends BaseFragment {

    @BindView(R.id.v_state)
    View mVState;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.iv_right_more)
    ImageView mIvRightMore;
    @BindView(R.id.rv_person_centre)
    RecyclerView mRvPersonCentre;
    @BindView(R.id.iv_left_back)
    ImageView mIvLeftBack;
    @BindView(R.id.iv_right_other)
    ImageView mIvRightOther;
    @BindView(R.id.civ_photo)
    CircleImageView mCivPhoto;
    @BindView(R.id.tv_person_name)
    TextView mTvPersonName;
    @BindView(R.id.tv_person_id)
    TextView mTvPersonId;
    @BindView(R.id.rb_person_grade)
    RatingBar mRbPersonGrade;
    @BindView(R.id.iv_qr_code)
    ImageView mIvQrCode;
    @BindView(R.id.tv_service_count)
    TextView mTvServiceCount;
    @BindView(R.id.iv_service_medal)
    ImageView mIvServiceMedal;
    @BindView(R.id.tv_ranking_service)
    TextView mTvRankingService;
    @BindView(R.id.tv_good_percent)
    TextView mTvGoodPercent;
    @BindView(R.id.iv_good_medal)
    ImageView mIvGoodMedal;
    @BindView(R.id.tv_ranking_good)
    TextView mTvRankingGood;
    @BindView(R.id.tv_count_grade)
    TextView mTvCountGrade;
    @BindView(R.id.iv_count_medal)
    ImageView mIvCountMedal;
    @BindView(R.id.tv_ranking_count)
    TextView mTvRankingCount;
    @BindView(R.id.tv_service_num)
    TextView mTvServiceNum;
    @BindView(R.id.tv_good_num)
    TextView mTvGoodNum;
    @BindView(R.id.tv_count_num)
    TextView mTvCountNum;

    @Override
    protected View bindView(ViewGroup container) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_person_centre, container, false);
        return view;
    }

    @Override
    protected void initView() {

        int stateHeight = DeviceUtils.getStateHeight(mContext);
        ViewGroup.LayoutParams params = mVState.getLayoutParams();
        params.height = stateHeight;
        mVState.setLayoutParams(params);

        mTvTitle.setText(ResourceUtils.getString(R.string.person_centre));

        mIvRightMore.setImageResource(R.drawable.ic_bar_setting);
        mIvRightMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(AppSettingActivity.class);
            }
        });

        mIvLeftBack.setVisibility(View.GONE);
        mIvRightOther.setVisibility(View.GONE);

        mRbPersonGrade.setStar(4);

        mIvServiceMedal.setVisibility(View.INVISIBLE);
        mTvServiceNum.setText(111 + "");
        mTvServiceNum.setTextColor(ResourceUtils.getColor(R.color.tv_medal_normal));

        mTvGoodNum.setText(1 + "");
        mTvGoodNum.setTextColor(ResourceUtils.getColor(R.color.tv_medal_well));


        mIvCountMedal.setImageResource(R.drawable.ic_silver_medal);
        mTvCountNum.setText(2 + "");
        mTvCountNum.setTextColor(ResourceUtils.getColor(R.color.tv_medal_well));

    }

    @Override
    protected void initData() {

        List<String> stringList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {

            stringList.add("时间：" + i);
        }
        MyPersonCentreAdapter adapter = new MyPersonCentreAdapter(stringList);
        mRvPersonCentre.setAdapter(adapter);
        mRvPersonCentre.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
//        adapter.setEmptyView(R.layout.item_load_empty, (ViewGroup) mRvPersonCentre.getParent());
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(final BaseQuickAdapter adapter, View view, final int position) {

                int[] location = new int[2];
                view.getLocationOnScreen(location);

                int width = view.getWidth();
                int height = view.getHeight();

                Log.d("--TAG--" + width + "===" + height, "MyTaskFragment onItemClick()" + location[0] + "===" + location[1]);

                TextView tvTest = new TextView(mContext);
                tvTest.setText("删除");
                tvTest.setGravity(Gravity.CENTER);
                tvTest.setBackgroundColor(ResourceUtils.getColor(R.color.white));

                final PopupWindow mPopupWindow = new PopupWindow(view);
                mPopupWindow.setWidth(width);
                mPopupWindow.setHeight(height);
                mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
                mPopupWindow.setOutsideTouchable(true);
                mPopupWindow.setFocusable(true);
                mPopupWindow.setContentView(tvTest);
                setPopupWindowBack(0.5F);
                mPopupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, 0, location[1]);
                mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {

                        setPopupWindowBack(1F);
                    }
                });

                tvTest.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        ToastUtils.showHint("点击了删除", mTvCountNum);
                        mPopupWindow.dismiss();
                        adapter.remove(position);
                    }
                });
            }
        });
    }

    private void setPopupWindowBack(Float alpha) {

        MainActivity activity = (MainActivity) mContext;
        Window window = activity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = alpha;
        window.setAttributes(lp);

    }

    @Override
    protected void initClick() {

        mIvQrCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(mContext,InviteCodeActivity.class));
            }
        });

        mCivPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(mContext,PersonInfoActivity.class));
            }
        });
    }
}
